package com.harvest.scientific.ladder.page.fa

import android.content.Intent
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.harvest.scientific.ladder.R
import com.harvest.scientific.ladder.base.BaseFragment
import com.harvest.scientific.ladder.databinding.FragmentMain2Binding
import com.harvest.scientific.ladder.ext.notEmpty
import com.harvest.scientific.ladder.ext.visible
import com.harvest.scientific.ladder.page.Info1Activity
import com.harvest.scientific.ladder.page.LoginActivity
import com.harvest.scientific.ladder.page.ui.main.HomeIntent
import com.harvest.scientific.ladder.page.ui.main.LoadUiState
import com.harvest.scientific.ladder.page.ui.main.MainViewModel
import com.harvest.scientific.ladder.util.LogUtils
import com.harvest.scientific.ladder.util.SpUtil
import com.harvest.scientific.ladder.util.VcUtil
import com.harvest.scientific.ladder.widget.BaseActionBar
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch


class HomeFragment : BaseFragment<FragmentMain2Binding>() {

    companion object {
        fun newInstance() = HomeFragment()
    }

    private val viewModel: MainViewModel by viewModels()


    override fun getTitle(): String {
        return getString(R.string.app_name)
    }

    override fun initPage() {

        initListener()

    }

    override fun setStatus(actionBar: BaseActionBar) {
        super.setStatus(actionBar)
        actionBar.apply {
            isShowBack(false)
            isShowBusinessServer(false)
        }
    }

    private fun initListener() {
        lifecycleScope.launch {

            launch {
                viewModel.uiStateFlow.collectLatest { state ->
                    when (state) {
                        is LoadUiState.Idle -> {

                        }

                        is LoadUiState.Loading -> {

                        }

                        is LoadUiState.GetAppSettings -> {
                            LogUtils.d("LoadUiState.GetMaxDay, max:${state}")
                            if (state.maxDay.notEmpty()) {
                                _binding.maxDayText.text =
                                    getString(R.string.montant_du_pr_t_jours, state.maxDay)
                            } else if (state.maxCredit.notEmpty()) {
                                _binding.maxCreditText.text = state.maxCredit
                            } else if (state.isBanner.notEmpty()) {
                                _binding.homeBanner1.visible(state.isBanner == "1")
                                _binding.homeBanner2.visible(state.isBanner == "1")
                            }
                        }


                        else -> {}
                    }

                }
            }
        }

        _binding.creditBtn.setOnClickListener {
            if (VcUtil.isFastDoubleClick(it)) {
                return@setOnClickListener
            }
            if (SpUtil.isLogin()) {
                startActivity(Intent(requireActivity(), Info1Activity::class.java))
            } else {
                startActivity(Intent(requireActivity(), LoginActivity::class.java))
            }
        }
    }

    override fun loadData() {
        viewModel.sendUiIntent(
            HomeIntent.QueryAppSettings(if (SpUtil.isTestCust() || !SpUtil.isLogin()) "unknownHatProbableScholar" else "tightBlueEyesight"))
        viewModel.sendUiIntent(
            HomeIntent.QueryAppSettings(if (SpUtil.isTestCust() || !SpUtil.isLogin()) "deliciousLeafDevelopment" else "prettyCrazyHerb")
        )
        viewModel.sendUiIntent(HomeIntent.QueryAppSettings("cloudyMidAshamedLieNose"))
    }

    override fun onResume() {
        super.onResume()
        loadData()
    }


}